#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110, M = 2010;
int h[N], e[M], ne[M], idx;
bool st[N];
int match[N];
int n, m, k;
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int find(int x)
{
    for (int i = h[x]; ~i; i = ne[i])
    {
        int j = e[i];
        if (st[j]) continue;
        st[j] = true;
        if (match[j] == 0 || find(match[j]))
        {
            match[j] = x;
            return true;
        }
    }
    return false;
}
int main()
{
    while (cin >> n, n)
    {
        cin >> m >> k;
        memset(h, -1, sizeof h);
        memset(match, 0, sizeof match);
        idx = 0;
        for (int i = 0; i < k; i++)
        {
            int t, a, b;
            cin >> t >> a >> b;
            if (a && b) add(a, b);
        }
        int res = 0;
        for (int i = 1; i < n; i++)
        {
            memset(st, false, sizeof st);
            if (find(i)) res++;
        }
        cout << res << endl;
    }
    return 0;
}